package com.baikeyang.service.activity.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baikeyang.common.constant.CommonConstants;
import com.baikeyang.common.model.ActivityUserJoin;
import com.baikeyang.common.model.UserJoinActivity;
import com.baikeyang.mapper.ActivityUserJoinMapper;
import com.baikeyang.mapper.UserJoinActivityMapper;
import com.baikeyang.service.activity.IActivityUserJoinService;
import com.baikeyang.service.base.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 参加报名活动的用户 服务实现类
 * </p>
 *
 * @author BaiKeyang
 * @since 2020-05-24
 */
@Service
public class IActivityUserJoinServiceImpl extends SuperServiceImpl<ActivityUserJoinMapper, ActivityUserJoin> implements IActivityUserJoinService {

    @Autowired
    private ActivityUserJoinMapper userJoinMapper;

    @Override
    public int saveActivityUserJoin(ActivityUserJoin userJoin) {
        // TODO 当前用户多次参加/取消该活动超过3次时，禁止参加活动
        QueryWrapper<ActivityUserJoin> queryWrapper = Wrappers.query();
        queryWrapper.eq("USER_ID", userJoin.getUserId());
        queryWrapper.eq("ACTIVITY_ID", userJoin.getActivityId());
        List<ActivityUserJoin> activityUserJoinList = userJoinMapper.selectList(queryWrapper);
        if(activityUserJoinList.size() >= 3){
            return 0;
        }
        userJoin.setCreateTime(new Date());
        userJoin.setCreateUser(0L);
        userJoin.setUpdateTime(new Date());
        userJoin.setUpdateUser(0L);
        return userJoinMapper.insert(userJoin);
    }

    @Override
    public int getActivityUserJoinByUserIdAndActivityId(Long userId, Long activityId) {
        QueryWrapper<ActivityUserJoin> queryWrapper = Wrappers.query();
        queryWrapper.eq("USER_ID", userId);
        queryWrapper.eq("ACTIVITY_ID", activityId);
        queryWrapper.eq("STATE", CommonConstants.ACTIVITY_STATUS_SUCCESS).or()  // 包含已通过
                .eq("STATE", CommonConstants.ACTIVITY_STATUS_AUDIT)   // 包含正在审核
                .or().eq("STATE", CommonConstants.ACTIVITY_STATUS_LOCK);   // 包含已锁定
        List<ActivityUserJoin> activityUserJoinList = userJoinMapper.selectList(queryWrapper);
        return activityUserJoinList.size();
    }

    @Override
    public int delActivityUserJoinByUserIdAndActivityId(String userId, Long activityId) {
        QueryWrapper<ActivityUserJoin> queryWrapper = Wrappers.query();
        queryWrapper.eq("USER_ID", userId);
        queryWrapper.eq("ACTIVITY_ID", activityId);
        queryWrapper.orderByDesc("CREATE_TIME");
        List<ActivityUserJoin> activityUserJoinList = userJoinMapper.selectList(queryWrapper);
        if(CollectionUtil.isNotEmpty(activityUserJoinList)){
            ActivityUserJoin activityUserJoin = activityUserJoinList.get(0);
            activityUserJoin.setUpdateTime(new Date());
            // TODO 默认正常通过
            activityUserJoin.setState(CommonConstants.ACTIVITY_STATUS_SUCCESS);
            return userJoinMapper.updateById(activityUserJoin);
        }
        return 0;
    }
}
